package com.example.miniodemo.controller;

import com.example.miniodemo.common.commondto.file.FileDownloadDTO;
import com.example.miniodemo.common.result.Result;
import com.example.miniodemo.pojo.dto.ArticleAddOrEditDTO;
import com.example.miniodemo.pojo.dto.ArticleListDTO;
import com.example.miniodemo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 文章表 前端控制层
 *
 * @author Lv
 * @date 2022/11/11
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     *
     * @param articleAddOrEditDTO 文章添加对象
     * @return
     */
    @PostMapping("/")
    public Result<?> add(@Validated ArticleAddOrEditDTO articleAddOrEditDTO) {
        return articleService.add(articleAddOrEditDTO);
    }

    /**
     * 查询文章列表
     *
     * @param articleListDTO 文章查询对象
     * @return
     */
    @GetMapping("/")
    public Result<?> list(@RequestBody ArticleListDTO articleListDTO) {
        return articleService.list(articleListDTO);
    }

    /**
     * 修改文章信息
     *
     * @param id                  文章id
     * @param articleAddOrEditDTO 文章修改对象
     * @return
     */
    @PutMapping("/{id}")
    public Result<?> edit(@PathVariable("id") Integer id, @Validated ArticleAddOrEditDTO articleAddOrEditDTO) {
        return articleService.edit(id, articleAddOrEditDTO);
    }

    /**
     * 获取文章详情
     *
     * @param id 文章id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> get(@PathVariable("id") Integer id) {
        return articleService.get(id);
    }

    /**
     * 下载文章
     *
     * @param fileDownloadDTO 文件下载数据传输对象
     * @return
     */
    @GetMapping("/download")
    public Result<?> download(@Validated @RequestBody FileDownloadDTO fileDownloadDTO) {
        return articleService.download(fileDownloadDTO);
    }
}
